package service.credit.impl;

import java.util.List;
import core.global.Global;
import dao.ApplystateDao;
import dao.CreditapplyenterpriseDao;
import exception.Exceptions;

import model.credit.Creditapplyenterprise;
import model.creditcheck.Applystate;
import service.credit.CreditApplyEnterpriseService;
import vo.CreditCheckBean;

public class CreditApplyEnterpriseServiceImpl implements CreditApplyEnterpriseService{
	private CreditapplyenterpriseDao creditapplyenterpriseDao;
	private ApplystateDao applystateDao;
	
	public ApplystateDao getApplystateDao() {
		return applystateDao;
	}

	public void setApplystateDao(ApplystateDao applystateDao) {
		this.applystateDao = applystateDao;
	}

	@Override
	public void save(
			Creditapplyenterprise creditapplyenterprise) throws Exceptions {
		try{
			this.creditapplyenterpriseDao.saveOrUpdate(creditapplyenterprise);
		}catch(Exception e){
			throw new Exceptions("在插入数据中出现了问题，请检查你的数据!");
		}
	}

	@Override
	public List<Creditapplyenterprise> getAllMyList() {
		return this.creditapplyenterpriseDao.findModel("customermanager = " + Global.userid(), 10);
	}

	@Override
	public Creditapplyenterprise getOneById(Integer id) {
		return this.creditapplyenterpriseDao.findOne("applyid", id);
	}

	public CreditapplyenterpriseDao getCreditapplyenterpriseDao() {
		return creditapplyenterpriseDao;
	}

	public void setCreditapplyenterpriseDao(
			CreditapplyenterpriseDao creditapplyenterpriseDao) {
		this.creditapplyenterpriseDao = creditapplyenterpriseDao;
	}

	@Override
	public List<?> getAllExamineList(String state) throws Exceptions {
		String select = "new vo.CreditCheckBean(c.applyid,p.name,c.loanaccount,c.applydate,c.applytime,c.applytimetype,c.creditstate, 'examine')";
		String from = "";
		try {
			if(state.equals("all")){
				//from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where c.clientid = p.id and c.creditstate = 11 and a.checktype = 'A' and a.managerid = " + Global.userid();
			}else if(state.equals("wait")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 21 and a.checktype = 'B' and a.managerid = " + Global.userid();
			}else if(state.equals("doing")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and (c.creditstate = 31 or c.creditstate = 32 or c.creditstate = 41) and a.checkstate <> 0 and a.checktype = 'B' and a.managerid = " + Global.userid();
			}else if(state.equals("back")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 12 and a.checkstate <> 0 and a.checktype = 'B' and a.managerid = " + Global.userid() + " and c.isdone >= 2";
			}else if(state.equals("returnBack")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 22 and a.checkstate <> 0 and a.checktype = 'B' and a.managerid = " + Global.userid() + " and c.isdone >= 3";
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.findQuery(select, from, 10);
	}
	
	/*得到所有审查的记录数据*/
	public int getNumberAllExamineList(String state) throws Exceptions{
		String from = "";
		try {
			if(state.equals("wait")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 21 and a.checktype = 'B' and a.managerid = " + Global.userid();
			}else if(state.equals("returnBack")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 22 and a.checkstate <> 0 and a.checktype = 'B' and a.managerid = " + Global.userid() + " and c.isdone >= 3";
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.countQuery(from);
	}

	@Override
	public List<?> getAllApprovalList(String state) throws Exceptions {
		String select = "new vo.CreditCheckBean(c.applyid,p.name,c.loanaccount,c.applydate,c.applytime,c.applytimetype,c.creditstate, 'approval')";
		String from = "";
		try {
			if(state.equals("all")){
				//from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 11 and a.checktype = 'C' and a.managerid = " + Global.userid();
			}else if(state.equals("wait")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 31 and a.checkstate = 0 and a.checktype = 'C' and a.managerid = " + Global.userid();
			}else if(state.equals("doing")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 41 and a.checkstate <> 0 and a.checktype = 'C' and a.managerid = " + Global.userid();
			}else if(state.equals("back")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and (c.creditstate = 12 or c.creditstate = 22) and a.checkstate <> 0 and a.checktype = 'C' and a.managerid = " + Global.userid() + " and c.isdone >= 3";
			}else if(state.equals("returnBack")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 32 and a.checkstate <> 0 and a.checktype = 'C' and a.managerid = " + Global.userid() + " and c.isdone >= 4";
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.findQuery(select, from, 10);
	}
	
	/*得到所有审批的记录数据*/
	@Override
	public int getNumberAllApprovalList(String state) throws Exceptions{
		String from = "";
		try {
			if(state.equals("wait")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 31 and a.checkstate = 0 and a.checktype = 'C' and a.managerid = " + Global.userid();
			}else if(state.equals("returnBack")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 32 and a.checkstate <> 0 and a.checktype = 'C' and a.managerid = " + Global.userid() + " and c.isdone >= 4";
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.countQuery(from);
	}

	@Override
	public List<?> getAllFinalApprovalList(String state) throws Exceptions {
		String select = "new vo.CreditCheckBean(c.applyid,p.name,c.loanaccount,c.applydate,c.applytime,c.applytimetype,c.creditstate, 'finalApproval')";
		String from = "";
		try {
			if(state.equals("all")){
				//from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where c.clientid = p.id and c.creditstate = 11 and a.checktype = 'A' and a.managerid = " + Global.userid();
			}else if(state.equals("wait")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and a.checktype = 'D' and c.creditstate = 41 and a.managerid = " + Global.userid();
			}else if(state.equals("doing")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and a.checktype = 'D' and c.creditstate = 0 and a.managerid = " + Global.userid();
			}else if(state.equals("back")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and a.checktype = 'D' and (c.creditstate = 12 or c.creditstate = 22 or c.creditstate = 32) and a.checkstate <> 0 and a.checktype = 'D' and a.managerid = " + Global.userid() + " and c.isdone >= 4";
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.findQuery(select, from, 10);
	}
	
	/*得到所有终审的记录数据*/
	@Override
	public int getNumberAllFinalApprovalList(String state) throws Exceptions{
		String from = "";
		try {
			if(state.equals("wait")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and a.checktype = 'D' and c.creditstate = 41 and a.managerid = " + Global.userid();
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.countQuery(from);
	}

	@Override
	public List<?> getAllAcceptionList(String state) throws Exceptions {
		String select = "new vo.CreditCheckBean(c.applyid,p.name,c.loanaccount,c.applydate,c.applytime,c.applytimetype,c.creditstate, 'accept')";
		String from = "";
		try {
			if(state.equals("all")){
				//from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where c.clientid = p.id and c.creditstate = 11 and a.checktype = 'A' and a.managerid = " + Global.userid();
			}else if(state.equals("wait")){
				from = "Creditapplyenterprise as c , Companybasic as p where c.clientid = p.id and c.creditstate = 1";
			}else if(state.equals("done")){
				from = "Creditapplyenterprise as c , Companybasic as p where c.clientid = p.id and c.creditstate <> 1";
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.findQuery(select, from, 10);
	}
	
	/*得到所有授理的记录数据*/
	@Override
	public int getNumberAllAcceptionList(String state) throws Exceptions{
		String from = "";
		try {
			if(state.equals("wait")){
				from = "Creditapplyenterprise as c , Companybasic as p where c.clientid = p.id and c.creditstate = 1";
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.countQuery(from);
	}
	
	@Override
	public CreditCheckBean getOneBean(Integer applyid) throws Exceptions{
		String select = "new vo.CreditCheckBean(c.applyid,c.clientid,p.name,c.loanofmode,c.loanaccount,c.applydate,c.applytime,c.applytimetype,c.applybaserate,c.slidingscale,c.performrates)";
		String from = "Creditapplyenterprise as c , Companybasic as p where c.applyid = " + applyid + " and c.clientid = p.id";
		List<?> lists;
		try {
			lists = this.creditapplyenterpriseDao.findQuery(select, from);
			if(lists != null){
				return (CreditCheckBean)lists.get(0);
			}else{
				return null;
			}
		} catch (Exception e) {
			throw new Exceptions("执行过程中出现了异常，请检查你的操作是否合法!");
		}
	}

	@Override
	public List<Creditapplyenterprise> getAllMyListByApplyDate(String applyDate) {
		return this.creditapplyenterpriseDao.findModel("customermanager = " + Global.userid() + " and applydate between '" + applyDate + "' and '" + applyDate +"'", 10);
	}

	@Override
	public List<Applystate> getOneApplystate(Integer id, String type) {
		return this.applystateDao.findModel("applyid = " + id + " and checktype = '" + type + "'");
	}

	@Override
	public int getCount(Integer applyid) {
		return creditapplyenterpriseDao.countModel("applyid = " + applyid);
	}

	@Override
	public void resetApply(Integer applyid) throws Exceptions {
		Creditapplyenterprise ae = this.creditapplyenterpriseDao.findOne("applyid", applyid);
		ae.setIsdone(0);
		ae.setCreditstate(11);
		try {
			ae.setCount(ae.getCount() + 1);
		} catch (Exception e) {
			throw new Exceptions("重设计数器失败!");
		}
		try {
			this.creditapplyenterpriseDao.save(ae);
		} catch (Exception e) {
			throw new Exceptions("重置过程中出现了一个问题!");
		}
		
	}

	@Override
	public List<?> getAllSurveyList(String state) throws Exceptions {
		String select = "new vo.CreditCheckBean(c.applyid,p.name,c.loanaccount,c.applydate,c.applytime,c.applytimetype,c.creditstate, 'survey')";
		String from = "";
		try {
			if(state.equals("all")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 11 and a.checktype = 'A' and a.managerid = " + Global.userid();
			}else if(state.equals("wait")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 11 and a.checkstate = 0 and a.checktype = 'A' and a.managerid = " + Global.userid();
			}else if(state.equals("done")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and (c.creditstate = 21 or c.creditstate = 22 or c.creditstate = 31 or c.creditstate = 32 or c.creditstate = 41) and a.checkstate <> 0 and a.checktype = 'A' and a.managerid = " + Global.userid();
			}else if(state.equals("returnBack")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 12 and a.checkstate <> 0 and c.isdone >= 2 and a.checktype = 'A' and a.managerid = " + Global.userid();
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.findQuery(select, from, 10);
	}
	
	/*得到所有调查的记录数据*/
	public int getNumberAllSurveyList(String state) throws Exceptions{
		String from = "";
		try {
			if(state.equals("wait")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 11 and a.checkstate = 0 and a.checktype = 'A' and a.managerid = " + Global.userid();
			}else if(state.equals("returnBack")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 12 and a.checkstate <> 0 and c.isdone >= 2 and a.checktype = 'A' and a.managerid = " + Global.userid();
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.countQuery(from);
	}

	@Override
	public List<?> getAllExamineAdviceList(String state) throws Exceptions {
		String select = "new vo.CreditCheckBean(c.applyid,p.name,c.loanaccount,c.applydate,c.applytime,c.applytimetype,a.checkstate, 'advice')";
		String from = "";
		try {
			if(state.equals("all")){
				//from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 11 and a.checktype = 'A' and a.managerid = " + Global.userid();
			}else if(state.equals("wait")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and a.checkstate = 0 and c.creditstate <> 0 and c.creditstate <> 10 and a.checktype = 'BB' and a.managerid = " + Global.userid();
			}else if(state.equals("done")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and a.checkstate = 1 and a.checktype = 'BB' and a.managerid = " + Global.userid();
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.findQuery(select, from, 10);
	}
	
	/*得到所有复审的记录数据*/
	public int getNumberAllExamineAdviceList(String state) throws Exceptions{
		String from = "";
		try {
			if(state.equals("wait")){
				from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and a.checkstate = 0 and c.creditstate <> 0 and c.creditstate <> 10 and a.checktype = 'BB' and a.managerid = " + Global.userid();
			}else{
				throw new Exceptions("查询不合法!");
			}
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.countQuery(from);
	}

	@Override
	public List<?> getAllAcceptionListByApplyIdAndState(Integer applyid, String state)
			throws Exceptions {
		String select = "new vo.CreditCheckBean(c.applyid,p.name,c.loanaccount,c.applydate,c.applytime,c.applytimetype,c.creditstate, '" + state + "')";
		String from = "";
		try {
		if(state.equals("accept")){
			from = "Creditapplyenterprise as c , Companybasic as p where c.clientid = p.id and c.applyid = " + applyid;
		}else if(state.equals("survey")){
			from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.applyid = " + applyid + " and a.checktype = 'A' and a.managerid = " + Global.userid();
		}else if(state.equals("examine")){
			from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.applyid = " + applyid + " and a.checktype = 'B' and a.managerid = " + Global.userid();
		}else if(state.equals("approval")){
			//from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.creditstate = 31 and a.checkstate = 0 and a.checktype = 'C' and a.managerid = " + Global.userid();
		}else if(state.equals("finalApproval")){
			from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.applyid = " + applyid + " and a.checktype = 'D' and a.managerid = " + Global.userid();
		}else if(state.equals("advice")){
			from = "Applystate as a, Creditapplyenterprise as c , Companybasic as p where a.applyid = c.applyid and c.clientid = p.id and c.applyid = " + applyid + " and a.checktype = 'BB' and a.managerid = " + Global.userid();
		}
			
		} catch (Exception e) {
			throw new Exceptions("查询不合法!");
		}
		if(from.equals("")){
			throw new Exceptions("查询不合法!");
		}
		return this.creditapplyenterpriseDao.findQuery(select, from);
	}

	@Override
	public Creditapplyenterprise getCreditapplyEnterpriseById(Integer id) {
		return this.creditapplyenterpriseDao.findOne("applyid", id);
	}

	@Override
	public void deleteById(Integer applyid) throws Exceptions {
		Creditapplyenterprise creditapplyenterprise = this.getCreditapplyEnterpriseById(applyid);
		if(creditapplyenterprise != null){
			try{
				this.creditapplyenterpriseDao.delete(creditapplyenterprise);
			}catch(Exception e){
				throw new Exceptions("删除过程中出现问题！，请检查信息的完整性！");
			}
		}
	}
}
